package com.gadberry.utility.expression.symbol;

import java.util.List;

import com.gadberry.utility.algorithm.Algorithm;
import com.gadberry.utility.expression.Argument;
import com.gadberry.utility.expression.Expression;
import com.gadberry.utility.expression.InvalidArgumentsException;

public class AssignmentSymbol extends TwoArgumentsSymbol {

	public AssignmentSymbol(Expression expression) {
		super(expression);
	}

	@Override
	public int getPriority() {
		return 1;
	}

	@Override
	public Argument resolve(Algorithm algor) {
		if(algor != null){
			String var = getArgument(0).toString();
			algor.setVariable(var, getArgument(1));
		}
		return getArgument(1);
	}

	@Override
	protected void checkArgs(List<Argument> args)
	throws InvalidArgumentsException {
		super.checkArgs(args);
		if (!args.get(0).isLiteral()) {
			throw new InvalidArgumentsException(
					"Assignment requires an identifier on LHS e.g. 'anIdentifier' :=  ...");
		}
		if(!args.get(1).isResolved()) {
			throw new InvalidArgumentsException(
			"RHS must be a valid data type");
		}
	}

}
